# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=wkhtmltopdf
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
_basever=0.13
pkgver=0.13.r925.e03f1d1
pkgrel=1
pkgdesc="Convert HTML to PDF using QtWebkit (mingw-w64)"
arch=('any')
url="http://wkhtmltopdf.org/"
license=('LGPLv3')
makedepends=("git" "${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-qt5"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=("${_realname}::git+https://github.com/wkhtmltopdf/wkhtmltopdf.git#branch=${_basever}"
        "${_realname}-${_basever}.patch")
sha256sums=('SKIP'
            '4671af5d8bc79102531c946b8a8f559d48f8000c0bdfd3d16eaedf3a71b5d243')
           
pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.r%s.%s" "${_basever}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"
  patch -Np1 -i "../${_realname}-${_basever}.patch"
}

build() {
  cd "${srcdir}/${_realname}"

  ln -fs `which windres` ./${MINGW_CHOST}-windres
  export PREFIX=${MINGW_PREFIX}
  PATH=`pwd`:$PATH

  # detect architecture
  if [[ ${MINGW_CHOST} == *"i686"* ]]; then
    target=mingw-w64-cross-win32
  else
    target=mingw-w64-cross-win64
  fi

  ./scripts/build.py ${target}
}

package() {
  cd "${srcdir}/${_realname}"
  
  # detect architecture
  if [[ ${MINGW_CHOST} == *"i686"* ]]; then
    target=mingw-w64-cross-win32
  else
    target=mingw-w64-cross-win64
  fi

  install -D ./include/wkhtmltox/dllbegin.inc "${pkgdir}${MINGW_PREFIX}/include/wkhtmltox/dllbegin.inc"
  install -D ./include/wkhtmltox/dllend.inc "${pkgdir}${MINGW_PREFIX}/include/wkhtmltox/dllend.inc"
  install -D ./include/wkhtmltox/image.h "${pkgdir}${MINGW_PREFIX}/include/wkhtmltox/image.h"
  install -D ./include/wkhtmltox/pdf.h "${pkgdir}${MINGW_PREFIX}/include/wkhtmltox/pdf.h"
  
  install -d "${pkgdir}${MINGW_PREFIX}/bin"
  install "./static-build/${target}/app/bin/"*.dll "${pkgdir}${MINGW_PREFIX}/bin"
  install "./static-build/${target}/app/bin/"*.exe "${pkgdir}${MINGW_PREFIX}/bin"
  
  install -d "${pkgdir}${MINGW_PREFIX}/lib"
  install "./static-build/${target}/app/bin/"*.a "${pkgdir}${MINGW_PREFIX}/lib"
}
